package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.service.EduVideoService;
import com.atguigu.serviceedu.vo.EduVideoInform;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2021-12-19
 */
@RestController
@RequestMapping("/serviceedu/video")
//@CrossOrigin
public class EduVideoController {
    @Autowired
    EduVideoService videoService;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "根据chapterId查询小节")
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfoById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id) {

        EduVideoInform eduVideoInform = videoService.getEduVideoInformById(id);
        return R.ok().data("video", eduVideoInform);
    }

    @ApiOperation(value = "根据ID修改小节")
    @PostMapping("updateVideo")
    public R updateById(@ApiParam(name = "chapter", value = "章节对象", required = true)
                        @RequestBody EduVideo video) {
        videoService.updateById(video);
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除小节")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id) {

        boolean result = videoService.removeVideoById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

}

